package com.hoshiicloud.market.service.impl.market;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.market.entity.market.Coupons;
import com.hoshiicloud.market.feign.IdGeneratorService;
import com.hoshiicloud.market.mapper.market.CouponsMapper;
import com.hoshiicloud.market.service.market.CouponsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * <p>
 * 系统优惠券 服务实现类
 * </p>
 *
 * @author campchen
 * @since 2019-05-14
 */
@Service
@Configuration
public class CouponsServiceImpl extends ServiceImpl<CouponsMapper, Coupons> implements CouponsService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
    * @param id
    * @return com.hoshiicloud.marketing.entity.Coupons
    * @description: 根据id查询系统优惠券
    */
    @Cacheable(cacheNames = "Coupons", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "营销服务-系统优惠券 -查询系统优惠券 ")
    public Coupons getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增系统优惠券
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Coupons", allEntries = true)
    @Logable(authName = "营销服务-系统优惠券-新增系统优惠券")
    public boolean save(Coupons entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改系统优惠券
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Coupons", allEntries = true)
    @Logable(authName = "营销服务-系统优惠券管理-修改系统优惠券")
    public boolean updateById(Coupons entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询系统优惠券
    */
    @Logable(authName = "营销服务-系统优惠券管理-分页查询系统优惠券")
    @Cacheable(cacheNames = "Coupons", key = "T(com.hoshiicloud.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ',queryWrapper=' + #queryWrapper.toString() + ')')")
    public IPage<Coupons> page(IPage<Coupons> page, Wrapper<Coupons> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Coupons", allEntries = true)
    @Logable(authName = "营销服务-系统优惠券管理-删除系统优惠券")
    public boolean deleteById(Coupons entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }
}
